package com.chang.dp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.chang.dp.entity.NoticeEntity;
import com.chang.dp.enumns.ErrorCodeEnums;
import com.chang.dp.mappers.NoticeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/notice")
@Api(tags = "公告模块")
public class NoticeController {

    @Autowired
    NoticeMapper noticeMapper;

    @RequestMapping(path = "/create", method = RequestMethod.POST)
    public HashMap<String, Object> createNotice(@RequestBody NoticeEntity noticeEntity) {
        HashMap<String, Object> result = new HashMap<>();
        noticeMapper.insert(noticeEntity);
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(path = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新公告")
    public HashMap<String, Object> updateNotice(@RequestBody NoticeEntity noticeEntity) {
        HashMap<String, Object> result = new HashMap<>();
        noticeMapper.insert(noticeEntity);
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(path = "/delete", method = RequestMethod.POST)
    @ApiOperation("删除公告")
    public HashMap<String, Object> deleteNotice(@RequestBody NoticeEntity noticeEntity) {
        HashMap<String, Object> result = new HashMap<>();
        noticeMapper.deleteById(noticeEntity.getId());
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(path = "/query", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询公告信息")
    public HashMap<String, Object> queryByPage(@RequestBody NoticeEntity noticeEntity) {
        HashMap<String, Object> result = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper<>(noticeEntity);
        PageDTO page = noticeMapper.selectPage(noticeEntity.getPagination(), queryWrapper);
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "查询成功");
        result.put("data", page);
        return result;
    }
}
